package com.bestdreamer.file;


import java.io.*;

/**
 * 文件夹复制
 */
public class CopyFile {

    public static void copyDir(String oldPath,String newPath) throws IOException {
        File file = new File(oldPath);
        String[] filePath = file.list();

        if(!(new File(newPath)).exists()){
            (new File(newPath)).mkdir();
        }

        for(int i =0; i < filePath.length; i++){
            if((new File(oldPath + File.separator + filePath[i])).isDirectory()){
                copyDir(oldPath + File.separator + filePath[i],newPath + File.separator + filePath[i]);
            }

            if(new File(oldPath + File.separator + filePath[i]).isFile()){
                copyFile(oldPath + File.separator + filePath[i],newPath + File.separator + filePath[i]);
            }
        }
    }

    /**
     * 复制文件的方法
     * @param oldPath
     * @param newPath
     * @throws IOException
     */
    public static void copyFile(String oldPath,String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);

        byte[] buffer = new byte[2097152];
        while((in.read(buffer)) != -1){
            out.write((buffer));
        }
    }

    public static void main(String[] args) throws IOException {
        copyDir("/Users/hkrong/workspace/src","/Users/hkrong/workspace/desc");
    }


}
